/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var username = document.getElementById("username");
var fullname = document.getElementById("fullname");
var password = document.getElementById("password");
var confirmPass = document.getElementById("confirm");
var email = document.getElementById("email");

var btRegister=document.getElementById("register");
var usernameInfo=document.getElementById("usernameInfo");
var fullnameInfo=document.getElementById("fullnameInfo");
var passwordInfo=document.getElementById("passwordInfo");
var confirmInfo=document.getElementById("confirmInfo");
var emailInfo=document.getElementById("emailInfo");
var captchaInfo = document.getElementById("captchaInfo");

var captchaChallenge=document.getElementById("recaptcha_challenge_field");
var captchaResponse=document.getElementById("recaptcha_response_field");

var USERNAME_REGREX=/^[a-zA-Z0-9_-]{3,16}$/;//Username tu 3 den 16 ky tu
var FULLNAME_REGEX=/^([a-zA-Z'-]+\s*){2,5}$/;
var PASSWORD_REGEX=/^.{6,16}$/; //Password từ 6 đến 16 char
var EMAIL_REGEX = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/; //viết theo thầy Khánh...

username.focus();
username.onblur=function(){
    checkUsername();
};
fullname.onblur=function(){
    checkFullname();
};
password.onblur=function(){
    checkPassword();
};
confirmPass.onblur=function(){
    checkConfirm();
};
email.onblur=function(){
    checkEmail();
};

btRegister.onclick=register;

var request=null;
function register(e){
    e.preventDefault();

    if (validateInput()){
        request=new XMLHttpRequest();
        var url="registerAction.action";
        var params = "username="+ username.value
        +"&fullname="+ fullname.value
        +"&password="+ password.value
        +"&email="+ email.value
        +"&challenge="+captchaChallenge.value
        +"&uresponse="+captchaResponse.value;

        request.open("POST", url,true);
        request.onreadystatechange=registerResult;
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(params);
    }
    
}

function registerResult(){
    if (request.readyState==4) {
        if (request.status==200) {
            var result = eval('('+ request.responseText +')');
            if (result.message=='success') {
                window.location.href="logined.jsp";
            } else if (result.message=='failureall') {
                usernameInfo.innerHTML=result.info.fontcolor("red");
                emailInfo.innerHTML=result.info.fontcolor("red");
            } else if (result.message=='failureusername') {
                usernameInfo.innerHTML=result.info.fontcolor("red");
            } else if (result.message=='failureemail') {
                emailInfo.innerHTML=result.info.fontcolor("red");
            } else if (result.message=='failurecaptcha') {
                captchaInfo.innerHTML=result.info.fontcolor("red");
                Recaptcha.reload();
            }
        }
    }
}

function validateInput(){
    var isValid=true;
    if (!checkUsername()) {
        isValid=false;
    }
    if (!checkFullname()) {
        isValid=false;
    }
    if (!checkPassword()) {
        isValid=false;
    }
    if (!checkConfirm()) {
        isValid=false;
    }
    if (!checkEmail()) {
        isValid=false;
    }
    return isValid;
}

function checkUsername(){
    usernameInfo.innerHTML='';
    if (!USERNAME_REGREX.test(username.value)) {
        usernameInfo.innerHTML='Tên đăng nhập phải dài từ 3 đến 16 ký tự.'.fontcolor("red");
        return false
    }
    return true
}

function checkFullname(){
    fullnameInfo.innerHTML='';
    if (!FULLNAME_REGEX.test(fullname.value)) {
        fullnameInfo.innerHTML='Tên đầy đủ phải gồm ít nhất 2 phần và chỉ chứa ký tự.'.fontcolor("red");
        return false
    }
    return true
}

function checkPassword(){
    passwordInfo.innerHTML='';
    if (!PASSWORD_REGEX.test(password.value)) {
        passwordInfo.innerHTML='Mật khẩu phải dài từ 6 đến 16 ký tự.'.fontcolor("red");
        return false
    }
    return true
}

function checkConfirm(){
    confirmInfo.innerHTML='';
    if (password.value!=confirmPass.value) {
        confirmInfo.innerHTML='Không giống mật khẩu phía trên.'.fontcolor("red");
        return false
    }
    return true
}

function checkEmail(){
    emailInfo.innerHTML='';
    if (!EMAIL_REGEX.test(email.value)) {
        emailInfo.innerHTML='Email sai định dạng.'.fontcolor("red");
        return false
    }
    return true
}